package com.easyhome.common.timer;

import com.easyhome.dw.timed.task.center.context.XxlJobHelper;
import com.easyhome.dw.timed.task.center.handler.IJobHandler;

import java.util.Objects;

/**
 * 支持灰度的job基类
 *
 * @author wangshufeng
 */
public abstract class JavaGrayProcessor extends IJobHandler {

    @Override
    public void execute() {
        try {
            JobContext jobContext = new JobContext(XxlJobHelper.getJobParam());
            this.preProcess(jobContext);
            ProcessResult result = this.process(jobContext);
            if (Objects.nonNull(result) && Objects.nonNull(result.getStatus()) && result.getStatus()) {
                XxlJobHelper.handleSuccess(result.getResult());
            } else {
                XxlJobHelper.handleFail(result.getResult());
            }
            this.postProcess(jobContext);
        } catch (Exception e) {
            XxlJobHelper.log("----------- XXLJob execute()执行失败。 -----------", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * job调用执行的方法
     *
     * @param jobContext
     * @return
     * @throws Exception
     */
    public abstract ProcessResult process(JobContext jobContext) throws Exception;

    public void preProcess(JobContext context) {
    }

    public ProcessResult postProcess(JobContext context) {

        return null;
    }

    public void kill(JobContext context) {
    }
}
